#!/usr/bin/python
# -*- coding: UTF-8 -*-
from UtilPackge import readExcel
from UtilPackge import Logger
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import os

def colorMainFun(browser,filePath):

    sheetName = 'ColorTypeData'
    DataExcel = readExcel.readExceInfo(filePath,sheetName)
    Logger.logger.info("获取到Sheet页面ColorTypeData的数据为："+str(DataExcel))

    #这里是循环写入产品的名称和勾选那些产品
    CheckProductsAndWriteProductName(browser,DataExcel)


#勾选产品和写入产品名称
def CheckProductsAndWriteProductName(browser,DataExcel):
    for item in range(len(DataExcel)):

        checkAndWrite(browser,item,DataExcel);

        cacheIMG(browser,item)

#勾选产品和写入产品名称
def checkAndWrite(browser,item,DataExcel):
    #勾选
    browser.find_element_by_xpath \
        ('//*[@id="struct-p-1627207"]/div/div[2]/div[1]/div/div/div[1]/div/div['
         +str(int(item)+1)+']/div/label/span[1]/input').click()

    #产品名称
    time.sleep(1)

    metaData = browser.find_element_by_xpath \
        ('//*[@id="struct-p-1627207"]/div/div[2]/div[1]/div/div/div[1]/div/div['
         +str(int(item)+1)+']/div/span/span/span/input').get_attribute("value")
    #根据源值的数量来删除源值
    for i in range(len(metaData)):
        browser.find_element_by_xpath \
            ('//*[@id="struct-p-1627207"]/div/div[2]/div[1]/div/div/div[1]/div/div['
             +str(int(item)+1)+']/div/span/span/span/input').send_keys(Keys.BACKSPACE)

    browser.find_element_by_xpath \
        ('//*[@id="struct-p-1627207"]/div/div[2]/div[1]/div/div/div[1]/div/div['
         +str(int(item)+1)+']/div/span/span/span/input') \
        .send_keys(str(DataExcel.get(item)[0]))


#缓存颜色分类图片
def cacheIMG(browser,colorTypeImgIndex):
    browser.find_element_by_xpath \
         ('//*[@id="struct-p-1627207"]/div/div[2]/div[1]/div/div/div[1]/div/div['
          +str(int(colorTypeImgIndex)+1)+']/div/div[1]/button').click()

    frame = browser.find_element_by_xpath \
        ("/html/body/div[14]/div/div[2]/div/div[2]/div/div/iframe" )
    browser.switch_to.frame(frame)
    time.sleep(2)
    #点击上传图片
    browser.find_element_by_xpath \
        ('//*[@id="container"]/div/div[2]/div[2]/div/div[1]/div[2]/div[1]/div[5]').click()
    time.sleep(2)
    #点击上传
    browser.find_element_by_id('uploadBtn').click()

    writeIMGIndex(colorTypeImgIndex);

    callAutoIt()
    time.sleep(10)
    browser.switch_to.default_content()

def writeIMGIndex(colorTypeImgIndex):

    fileOperation = open(r"C:\Users\hesy\Desktop\ImgCount.txt", "w")
    #写入之前清除
    fileOperation.truncate()
    Logger.logger.info("当前图片的坐标为："+str(colorTypeImgIndex))
    fileOperation.write(str(colorTypeImgIndex))
    fileOperation.close()

# 启动AutoIt来控制文件
def callAutoIt():
    # project_root_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    # colorAutoIt_path = project_root_path+r"\UtilPackge\AutoIt\colorTypeScript.au3"
    # time.sleep(5)
    os.system("F:\soft\MeSoft\AutoIT\AutoItWorks\colorTypeScript.exe")
